<?php

  class TixianAction extends CommonAction{
  
     public function index(){
	 
	   $where['tixianid']=array('eq',session('did'));
	 
	   $re=$this->page('tixian','10','id desc',$where);
	   
	   
	   //dump($re);
	   
	   $this->re=$re;
	   
	   
	       $my=M('dailiuser')->where('id="%s"',session('did'))->find();
		   
		   $keyword=$my['keyword'];
		   
		   $sys=M('system')->where('id=1')->find();
	   
	      require_once './phpqrcode/phpqrcode.php';
                        
		  $data = $sys['url']."/index.php/User/tixian/keyword/".$keyword;
	
		  $level = 'H';
	
		  $size = 6;
	
		  $path = "./tixianma/";
	
		  // 生成的文件名
	
		  $fileName = $path.$keyword.'.png';
	
		  QRcode::png($data, $fileName, $level, $size);
	   
	   $this->my=M('dailiuser')->where('id="%s"',session('did'))->find();
	   $this->display();
	 
	 }
	 
	 
	  public function savetixian(){
	 
	 
				//查询用户
				
				$user=M('dailiuser')->where('id="%s"',session('did'))->find();
				
				$mima=md5($_POST['mima']);
			/*	
				if($mima!=$user['tixianpwd']){
				
				  $ajax['msg']=11;
				  
				  $this->ajaxReturn($ajax);
				
				}
				*/
				
				//提现金额小于2元断掉
				
				if($_POST['money']<20){
				  
				    $ajax['msg']=5;
				   
				   $this->ajaxReturn($ajax);
				  
				
				}
				
				
				
				//余额是否大于提现金额
				if($user['money']<$_POST['money']){
				  
				   $ajax['msg']=3;
				   
				   $this->ajaxReturn($ajax);
				
				}
				
				
				//查询出最后一次写入的openid 如果不一致 提示不能提现
				
			/*	$tixianopenid=M('tixianzhong')->where('keyword="%s"',session('keyword'))->find();
				
				if(session('openid')!=$tixianopenid['openid']){
				
				    $ajax['msg']=4;
				    $this->ajaxReturn($ajax);
				}*/
				
				
				//首先扣除提现金额
				
				$st['money']=$user['money']-$_POST['money'];
				
				M('dailiuser')->where('id="%s"',session('did'))->save($st);
				
				
				
				
				
				//计算提成 扣除佣金
				
				$yongjin=M('yongjin')->where('id=1')->find();
				$sys=M('system')->where('id=1')->find();
				
				$kouchuyongjin=$sys['yongjin']/100;
				
				//dump($kouchuyongjin);
				
				$shijitixian=$_POST['money']-($_POST['money']*$kouchuyongjin);
				
				
				
				
			   //第一层上级返佣
				  if($user['upid']!=""){
				  
					   if($_POST['money']>=1){
				  
				  
						$top1=M('dailiuser')->where('id="%s"',$user['upid'])->find();
						
						
						//判断上级级别
						
						switch($top1['level']){
						
						  case "0":
						  
							$yongjin=$yongjin['yongjin'];
						  
						  break;
						  
						  case "1":
							
							$yongji=$yongjin['chujiyongjin'];
						  
						  break;
						  
						  case "2":
							
							$yongjin=$yongjin['gaojiyongjin'];
						  
						  break;
						
						
						}
						
						
					
						
						
						$yingfan=$_POST['money']*($yongjin/100);
						
						
						
						$yingfan=round($yingfan,2);
						
						
						
						$data['money']=$top1['money']+$yingfan;
						
					
						
						M('dailiuser')->where('id="%s"',$user['upid'])->save($data);
						
						
						//首先存入返佣表
						
					
						
						}
					
				  
					  }
							
				//返佣结束
				
				
				//纯利润
				
				$chunlirun=$_POST['money']*$kouchuyongjin-$yingfan;;
				
				
				
			
				
				
				$shijitixian=round($shijitixian,2);
				
				 $data['openid'] = $_POST['zhifubao'];
				 $data['alipayname'] = $_POST['alipayname'];
				 $data['time']  = time();
				 $data['money'] = $shijitixian;
				 $data['tixianid']=session('did');
				 $data['shoufanyongid']=$user['upid'];
				 $data['yongjin']=$yingfan;
				 $data['maolirun']=$_POST['money']*$kouchuyongjin;
				 $data['chunlirun']=$chunlirun;
				 $data['status']=2;
				 $re = M('tixian')->add($data);
				 // $data['openid']=session('openid');
				  $ajax['msg'] =0;
				  $this->ajaxReturn($ajax);
		
		
	 }
	 
	 
	 
	
  
  
  
  }